package com.seqb.manage.video.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.reptile.service.impl.HxcVideoServiceImpl;
import com.seqb.manage.video.domain.VideoDO;
import com.seqb.manage.video.service.VideoService;
import com.seqb.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lilinshen
 * @title 视频表
 * @description 请填写相关描述
 * @date 2020-05-15
 */
@Controller
@RequestMapping("/manage/video/video")
public class VideoController extends BaseController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private HxcVideoServiceImpl hxcVideoService;

    @GetMapping()
    @Log("视频表列表")
    String Video() {
        return "manage/video/video.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @Log("视频表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<VideoDO> page = videoService.list(query);
        PageUtils pageUtils = new PageUtils(page.getResult(), page.getTotal());
        return pageUtils;
    }

    @GetMapping("/add")
    @Log("视频表添加")
    String add() {
        return "manage/video/video.add";
    }

    @GetMapping("/edit/{id}")
    @Log("视频表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        VideoDO video = videoService.get(id);
        model.addAttribute("video", video);
        return "manage/video/video.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @Log("视频表添加保存")
    public ResultVo save(VideoDO video) {
        if (videoService.save(video) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("视频表编辑保存")
    public ResultVo update(VideoDO video) {
        videoService.update(video);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @Log("视频表删除")
    public ResultVo remove(Long id) {
        if (videoService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @Log("视频表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        videoService.batchRemove(ids);
        return ResultVo.ok();
    }

    @GetMapping("/view/{id}")
    @Log("视频表查看")
    String view(@PathVariable("id") Long id, Model model) {
        VideoDO video = videoService.get(id);
        model.addAttribute("video", video);
        return "manage/video/video.view";
    }

    @ResponseBody
    @RequestMapping("/reptile")
    @Log("爬取含羞草视频")
    ResultVo reptile() {
        hxcVideoService.reptileHxcVideo();
        return ResultVo.ok();
    }

}